package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.CommunityDao;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2022-09-17 23:13 星期六
 * description:
 */
@Service(interfaceClass = CommunityService.class)
@Transactional
public class CommunityServiceImpl extends BaseServiceImpl<Community> implements CommunityService {

    @Autowired
    private CommunityDao communityDao;

    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<Community> getEntityDao() {
        return communityDao;
    }

    @Override
    public List<Community> findAll() {
        return communityDao.findAll();
    }

    //单表查询查询次数多  效率较低
//    @Override
//    public PageInfo<Community> findPage(Map<String, Object> filters) {
//        //当前页数
//        int pageNum = CastUtil.castInt(filters.get("pageNum"), 1);
//        //每页显示的记录条数
//        int pageSize = CastUtil.castInt(filters.get("pageSize"), 10);
//        PageHelper.startPage(pageNum, pageSize);
//
//        Page<Community> page = getEntityDao().findPage(filters);
//
//        //根据areaId 和 plateId 获取  areaName 和 plateName
//        List<Community> communityList = page.getResult();
//        for (Community community : communityList) {
//            //获取Dict
//            Dict dict = dictDao.getById(community.getAreaId());
//            community.setAreaName(dict.getName());
//
//            dict = dictDao.getById(community.getPlateId());
//            community.setPlateName(dict.getName());
//        }
//
//        //page转换为pageInfo并返回
//        return new PageInfo<Community>(page, 10);
//    }


    @Override
    public Community getById(Serializable id) {

        //这里需要调父类中的getById()方法  否则会产生递归调用  发生栈溢出错误
        Community community = super.getById(id);

        //areaId --> areaName
        Dict dict = dictDao.getById(community.getAreaId());
        community.setAreaName(dict.getName());

        //plateId --> plateName
        dict = dictDao.getById(community.getPlateId());
        community.setPlateName(dict.getName());

        return community;
    }
}
